package dbx

import (
	"net/http"

	"github.com/elastic/go-elasticsearch/v9"
)

type ElasticsearchSet struct {
	DataBase ReplicaSet `json:"database" yaml:"database" toml:"database"`
	TLS      TLS        `json:"tls,omitempty" yaml:"tls,omitempty" toml:"tls,omitempty"`
}

func (r ElasticsearchSet) Option(handles ...func(*elasticsearch.Config)) elasticsearch.Config {
	options := elasticsearch.Config{
		Addresses:                r.DataBase.Addrs,
		Username:                 r.DataBase.UserName,
		Password:                 r.DataBase.Password,
		CloudID:                  "",
		APIKey:                   "",
		ServiceToken:             "",
		CertificateFingerprint:   "",
		Header:                   nil,
		CACert:                   nil,
		RetryOnStatus:            nil,
		DisableRetry:             true,
		MaxRetries:               0,
		RetryOnError:             nil,
		CompressRequestBody:      false,
		CompressRequestBodyLevel: 0,
		PoolCompressor:           false,
		DiscoverNodesOnStart:     false,
		DiscoverNodesInterval:    0,
		EnableMetrics:            true,
		EnableDebugLogger:        true,
		EnableCompatibilityMode:  true,
		DisableMetaHeader:        false,
		RetryBackoff:             nil,
		Transport:                nil,
		Logger:                   nil,
		Selector:                 nil,
		ConnectionPoolFunc:       nil,
		Instrumentation:          nil,
	}
	if r.TLS.IsEnable() {
		options.Transport = &http.Transport{
			TLSClientConfig: r.TLS.Tls(),
		}
	}

	for _, handle := range handles {
		handle(&options)
	}

	return options
}
